{%extends 'admin/layout.html'%}
{%block content%}
<div class="mdui-container-fluid">
    <div class="mdui-typo">
        <h1> 离线下载 <small>远程文件->服务器->onedrive</small></h1>
    </div>
    <div class="mdui-toolbar mdui-color-theme">
      <button class="mdui-btn mdui-btn-icon" onclick="AddLink()" mdui-tooltip="{{'{'}}content: '添加任务'{{'}'}}"><i class="mdui-icon material-icons">&#xe250;</i></button>
      <button class="mdui-btn mdui-btn-icon" onclick="window.open('http://www.torrent.org.cn/');" mdui-tooltip="{{'{'}}content: '种子转磁力'{{'}'}}"><i class="mdui-icon material-icons">&#xe428;</i></button>
      <button class="mdui-btn mdui-btn-icon" onclick="ClearHist('{{GetConfig("admin_prefix")}}')" mdui-tooltip="{{'{'}}content: '清空任务'{{'}'}}"><i class="mdui-icon material-icons">&#xe0b8;</i></button>
    </div>
    <div class="mdui-row">
        <div class="mdui-col-xs-3">
            <label class="mdui-textfield-label">先选择网盘</label>
            <select class="mdui-select" id="user" mdui-select onchange="UserChange()">
                {%for user in get_od_user(True)%}
                {%if user[1]!='添加网盘'%}
                <!-- <option value="{{user[0]}}">{{user[1]}}</option> -->
                <option value="{{user[0]}}" id="user_{{user[0]}}">{{user[1]}}</option>
                {%endif%}
                {%endfor%}
            </select>
        </div>
        <div class="mdui-col-xs-6">
            <label class="mdui-textfield-label">再选择下载路径</label>
            <select class="mdui-select" id="path" mdui-select onchange="PathChange()">
            </select>
        </div>
        <div class="mdui-col-xs-3"></div>
    </div>
    <div class="nexmoe-item">
        <div class="mdui-row">
            <ul class="mdui-list">
                <li class="mdui-list-item th">
                    <div class="mdui-col-xs-12">活动任务</div>
                </li>
                <div class="mdui-panel mdui-panel-gapless" mdui-panel id="active">
                </div>
            </ul>
            <ul class="mdui-list">
                <li class="mdui-list-item th">
                    <div class="mdui-col-xs-12">失败任务</div>
                </li>
                <div class="mdui-panel mdui-panel-gapless" mdui-panel id="fail">
                </div>
            </ul>
            <ul class="mdui-list">
                <li class="mdui-list-item th">
                    <div class="mdui-col-xs-12">完成任务</div>
                </li>
                <div class="mdui-panel mdui-panel-gapless" mdui-panel id="success">
                </div>
            </ul>
        </div>
    </div>

</div>
<script type="text/javascript" src="/static/js/offdownload.js?t=20190525v2"></script>
<script>
{%if msg!=None%}
ShowNotice("{{msg}}");
{%endif%}
function GetTreelist(user){
    $.ajax({
        type: "GET",
        url: "/{{GetConfig('admin_prefix')}}/treelists/"+user,
        async: false, //使用同步的方式,true为异步方式
        dataType: "json",
        success: function(data) {
            var inst = new mdui.Select("#path");
            $("#path").empty();
            for (var i = 0; i <= data.length - 1; i++) {
                f=data[i];
                html='<option value="'+f['path']+'" ';
                if (f['path']=="{{cur_path}}"){
                    html+='selected';
                }
                html+=' id="path_'+f['path']+'"';
                html+='>'+f['split']+f['path']+'</option>';
                $("#path").append(html);
            }
            inst.handleUpdate();
        },
        complete: function(xhr) {
        }
    });
}
function UserChange(){
    user=$("#user").val();
    GetTreelist(user);
}
function PathChange(){
    path=$("#path").val();
    if (path) {
        window.location.href="#"+path;
    }
}
function AddLink() {
    layer.prompt({
        formType: 2,
        title: '请输入下载链接（一行一个）',
        maxlength:10000
    },
    function(value, index, elem) {
        layer.close(index);
        var defer = $.Deferred();
        var index2 = layer.load(2, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        $.ajax({
            type: "POST",
            url: "/{{GetConfig('admin_prefix')}}/off_download",
            data: { grand_path: $("#path").val(), urls: value, user: $("#user").val() },
            // async: false, //使用同步的方式,true为异步方式
            dataType: "json",
            success: function(data) {
                defer.resolve(data);
                if (data.status) {
                    ShowNotice('添加任务成功！');
                } else {
                    ShowNotice(data.msg);
                }
                layer.close(index2);
            }
        });
    });
}

$( document ).ready(function() {
    var path = $(location).attr('hash').replace('#', '');
    if (path){
        path=unescape(path);
        console.log(path);
        var user=path.split(':')[0];
        GetTreelist(user);
        var user_inst = new mdui.Select("#user");
        var path_inst = new mdui.Select("#path");
        $("#user").val(user);
        $("#path").val(path);
        user_inst.handleUpdate();
        path_inst.handleUpdate();
    }
    else{
        GetTreelist("{{cur_user}}");
    }
});

function Refresh(){
    tellActive("{{GetConfig('admin_prefix')}}");
}
var interval_id=null;
function AutoRefresh(){
    if (interval_id){
        window.clearInterval(interval_id);
    }
    interval_id = window.setInterval(function(){
        if(need_refresh){
            Refresh();
        }
    },2000);
}
AutoRefresh();

</script>
{%endblock content%}
